OpenRoads Designer CONNECT Edition SDK Help

Print point & component names from template definition of a template drop

When a corridor has assigned a template, the template definition includes the points and components definitions. The below code snippet shows how to print the point and component names by iterating through the template definition of a template drop.

Example


public void PrintTemplateProperties(Corridor corridor)
        {
            //Iterate over all TemplateDrops of a corridor 
            foreach (TemplateDrop td in corridor.TemplateDrops)
            {
                //Get xmlFragment string from Template of Template drop 
                string xmlFragment = td.Template.XMLFragment;

                //Load the xmlFragment into XMLDocument to read
                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.LoadXml(xmlFragment);

                //Itearate over all child nodes of xml document
                foreach (XmlNode node in doc.ChildNodes)
                {
                    //select and iterate over all nodes from current node having node name 
                    //equals to "Points"
                    XmlNodeList pointsNodeList = node.SelectNodes("Points");
                    if (pointsNodeList != null)
                    {
                        foreach (XmlNode pointsNode in pointsNodeList)
                        {
                            Console.WriteLine("Points");
                            foreach (XmlNode pointNode in pointsNode.ChildNodes)
                            {
                                //Read the "Point" node
                                XmlElement pointEle = pointNode as XmlElement;
                                if (pointEle != null)
                                {
                                    //Print the point name to console
                                    string pointNameAttribute = pointEle.GetAttribute("name");
                                    Console.WriteLine("Point name: " + pointNameAttribute);
                                }
                            }
                        }
                    }

                    //select and iterate over all nodes from current node having node name 
                    //equals to "Components"
                    XmlNodeList componentsNodeList = node.SelectNodes("Components");
                    if (componentsNodeList != null)
                    {
                        foreach (XmlNode componentsNode in componentsNodeList)
                        {
                            Console.WriteLine("Components");
                            foreach (XmlNode componentNode in componentsNode.ChildNodes)
                            {
                                //Read the "Component" node
                                XmlElement componentEle = componentNode as XmlElement;
                                if (componentEle != null)
                                {
                                    //Print the component name to console
                                    string componentNameAttribute = componentEle.GetAttribute("name");
                                    Console.WriteLine("Component name: " + componentNameAttribute);
                                }
                            }
                        }
                    }
                }
            }
        }

The templates Template.XMLFragment field stores the template as string. User can load this string into new System.Xml.XmlDocument object and iterate over this xml document to get all points and components.